package com.source.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Role;
import com.source.sys.entity.UserRole;
import com.source.sys.mapper.UserRoleMapper;
import com.source.sys.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.sys.vo.UserRoleVo;
import com.source.utils.MyPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public Page<UserRoleVo> listUserRolePage(int currentPageNo, int size) {
//        List<Map<String, Object>> mapList = userRoleMapper.listUserRoleInfo();
//        Page<List<Map<String, Object>>> pages = MyPage.list2Page(currentPageNo, size, mapList);
        return userRoleMapper.listUserRoleInfo(new Page<>(currentPageNo,size));

    }

    @Override
    public boolean addUserRole(UserRole userRole) {
        return userRoleMapper.insert(userRole)>0?true:false;
    }

    @Override
    public UserRole showUserRole(String userId) {
        return userRoleMapper.selectById(userId);
    }

    @Override
    public UserRoleVo showUserRoleInfo(String userId) {
        return userRoleMapper.getUserRoleInfoById(userId);
    }

    @Override
    public boolean updateUserRole(UserRole userRole) {
        return userRoleMapper.updateById(userRole)>0?true:false;
    }

    @Override
    public String getRoleIdByUserId(String userId) {
        UserRole userRole = userRoleMapper.selectOne(new QueryWrapper<UserRole>().eq("userId", userId));
        if (userRole != null){
            return userRole.getRoleId();
        }else{
            return null;
        }
    }

    @Override
    public Role getRoleByUserId(String userId) {
        return userRoleMapper.getRoleByUserId(userId);
    }

    @Override
    public Page<UserRoleVo> likeSearchUserRolePage(String value,int no,int size) {
        Page<UserRoleVo> result = userRoleMapper.likeSearchUserRolePage(value, new Page<>(no, size));
        return result;

    }
}
